function Snake(){}
// 初始化 ;
Snake.prototype.init = function(){
      // 获取屏幕宽高;
      var s_w = document.body.clientWidth;
      var s_h = document.body.clientHeight;
      // 初始化贪食蛇容器宽高
      this.snake_container = document.querySelector(".snake-container");
      // 计算列数;
      this.column = parseInt(s_w / 100);
      // 计算行数
      this.line   = parseInt( s_h / 100 );
      // 计算列数;
      this.snake_container.style.width  = this.column * 100 + "px";
      this.snake_container.style.height = this.line * 100 + "px";
      // 创建网格
      this.createGrid( this.column , this.line );
      // 贪食蛇运动;
      // 贪食蛇运动数据;
      this.direction = "right";
      this.x = 0 ;
      this.y = 0 ;
      this.snake_time = null;
      this.snake = document.querySelector(".snake");
      this.snakeMove();
      // 贪食蛇遥控器;
      this.snakeControl();
      // 创建随机蛋
      // 蛋数据;
      this.egg_timer = null;
      this.createEgg();
}
Snake.prototype.createGrid = function( ){
      var tr = "";
      for(var i = 0 ; i < this.line ; i ++){
            tr += "<tr>";
            for(var k = 0 ; k < this.column ; k ++){
                 tr += "<td></td>";
            }
            tr += "</tr>";
      }
      document.querySelector(".snake-grid").innerHTML = tr;
}
Snake.prototype.snakeMove = function(){
      var _this = this;
      // 蛇移动锁
      this.lock = false;
      this.snake_time = setInterval( function(){
            _this.lock = false;
            switch(_this.direction){
                  case "right" : 
                        _this.x ++ ;
                        break;
                  case "left" : 
                        _this.x --;
                        break;
                  case "bottom":
                        _this.y ++;
                        break;
                  case  "top" : 
                        _this.y --;
                        break;
            }
            // 终止判定;
            if(_this.x < 0){
                  _this.gameOver();
            }
            if(_this.x > _this.column - 1){
                  _this.gameOver();
            }
            if(_this.y < 0){
                  _this.gameOver();
            }
            if(_this.y > _this.line - 1){
                  _this.gameOver();
            }
            _this.snake.style.left = _this.x * 100 + "px";
            _this.snake.style.top  = _this.y * 100 + "px";

            _this.eat( _this.x  , _this.y);
            _this.follow();
      } , 200)
}

Snake.prototype.follow = function(){
      var bodys = document.querySelectorAll(".snake-body");
      if(bodys.length === 0) return false;
      for(var i = bodys.length - 1 ; i > 0 ; i --){
            bodys[i].style.left = bodys[i-1].style.left;
            bodys[i].style.top = bodys[i-1].style.top;
      }
      bodys[0].style.left = this.snake.style.left;
      bodys[0].style.top  = this.snake.style.top;
}

Snake.prototype.eat = function(x  , y ){
      var egg = document.querySelector(".egg");
      //防止报错;
      if(!egg) return false;
      if(egg.x == x && egg.y == y){
            egg.remove();
            this.createEgg(true);
            this.add();
      }
}

Snake.prototype.add = function(){
      var snake_body = document.createElement("div");
      snake_body.className = "snake-body"
      snake_body.style.left = this.x * 100 + "px";
      snake_body.style.top  = this.y * 100 + "px";
      this.snake_container.appendChild(snake_body);
}

Snake.prototype.createEgg = function(fast){
      var _this = this;
      // 快速创建模式;
      if(fast){
            create();
      }

      clearInterval(this.egg_timer);
      this.egg_timer = setInterval( create , 2000);
      function create(){
            var last_egg = document.querySelector(".egg");
            last_egg ? last_egg.remove() : "";
            // 创建随机坐标;
            var x = Math.round(Math.random() * (_this.column - 1))
            var y = Math.round(Math.random() * (_this.line - 1));
            // 创建蛋元素;
            var egg = document.createElement("div");
            egg.className    = "egg";
            egg.style.left  = x * 100 + "px";
            egg.style.top = y * 100 + "px";
            // 标记蛋的位置;
            egg.x = x;
            egg.y = y;
            _this.snake_container.appendChild(egg);
      } 
}

Snake.prototype.gameOver = function(){
      alert("游戏终止");
      clearInterval(this.snake_time);
      clearInterval(this.egg_timer);
      throw "游戏结束";
}
Snake.prototype.snakeControl = function(){
      var _this = this;
      document.onkeydown = function(evt){
            // 防止连续按键;
            // if(_this.lock){
            //       return false;
            // }
            // _this.lock = true;
            var code = evt.keyCode;
            // console.log(code);
            switch(code){
                  case 37 : 
                        if( _this.direction !== "right"){
                              _this.direction = "left";
                        }
                        break;
                  case 38 : 
                        if( _this.direction !== "bottom"){
                              _this.direction = "top";
                        }
                        break;
                  case 39 : 
                        if( _this.direction !== "left"){
                              _this.direction = "right";
                        }
                        break;
                  case 40 : 
                        if( _this.direction !== "top"){
                              _this.direction = "bottom";
                        }
                        break;
            }
      }
}

new Snake().init();